var express = require('express');
var router = express.Router();
var _ = require('underscore');
var site_info =	require("../config").site_info;
var fs = require('fs');
var path = require('path');
var images = require('images');

/* GET home page. */
router.get('/', function(req, res, next) {
	var cur_site_info = _.extend({}, site_info, {
        page:{
            title: "admin - " + site_info.page.title,
            name: "admin",
            intro: "",
            intro_desc: "",
        }
    });

	res.render('admin', {
		site_info: cur_site_info
	});
});

router.post('/upload', function(req, res, next) {    
    var files = req.files || [];
    if(req.files){
        var fileInfo = req.files.Filedata;

        var target_path = "./upload/" + fileInfo.originalFilename;
        if(fs.existsSync(target_path)){
            var extname = path.extname(fileInfo.originalFilename);
            target_path = "./upload/" 
                + path.basename(fileInfo.originalFilename, extname)
                + "_" + new Date().getTime()
                + extname;
        }
        
        fs.rename(fileInfo.path, target_path, function (err) {
            if (err) next(err);

            //fs.unlink(fileInfo.path);  

            return res.json({
                status:site_info.return_type.success, result:{
                path: target_path,
                name: fileInfo.originalFilename,
                size: fileInfo.size,

            }});

        });
    }
    else{
        return res.json({status:site_info.return_type.error, message:"not any files post", result:""});
    }

});

router.post('/upload-logo', function(req, res, next) {    
    var files = req.files || [];
    if(req.files){
        var fileInfo = req.files.Filedata;
        var extname = "";
        var imageExts = [".jpg", ".jpge", ".png", ".gif"];

        var target_path = "./public/images/profile_o/" + fileInfo.originalFilename;
        if(fs.existsSync(target_path)){
            extname = path.extname(fileInfo.originalFilename);
            target_path = "./public/images/profile_o/"
                + path.basename(fileInfo.originalFilename, extname)
                + "_" + new Date().getTime()
                + extname;
        }
        var width=0, height=0;
        if(imageExts.indexOf(extname) != -1){
            var image = images(fileInfo.path);
            width = image.width();
            height = image.height();
        }
        
        fs.rename(fileInfo.path, target_path, function (err) {
            if (err) next(err);

            //fs.unlink(fileInfo.path);  

            return res.json({
                status:site_info.return_type.success, result:{
                path: target_path.replace('./public',''),
                name: fileInfo.originalFilename,
                size: fileInfo.size,
                width: width,
                height: height
            }});

        });
    }
    else{
        return res.json({status:site_info.return_type.error, message:"not any files post", result:""});
    }

});


router.get('/upload-logo/cut', function(req, res, next) {  
    var imgInfo = {
        path: req.query.path || '',
        x: req.query.corrdinateX || 0,
        y: req.query.corrdinateY || 0,
        width: req.query.imageWidth || 0,
        height: req.query.imageHeight || 0,
    };

    var target_path = "./public" + imgInfo.path;
    if(imgInfo.path == "" || !fs.existsSync(target_path) || 
        imgInfo.width== "0" || imgInfo.width == "" || 
        imgInfo.height== "0" || imgInfo.height == ""){
        return res.jsonp({status:site_info.return_type.error, message:"image info has error"})
    }

    images(images(target_path), 
        parseInt(imgInfo.x), parseInt(imgInfo.y),
        parseInt(imgInfo.width), parseInt(imgInfo.height))
    .save('./public/images/profile/' + path.basename(target_path), {
        quality : 50
    });

    return res.json({
        status:site_info.return_type.success, result:{
            path: target_path
        }
    });
});

module.exports = router;
